package com.angcyo.selenium.bean

import com.angcyo.http.base.copyByJson

/**
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/21
 */
class DslTask {

    var getBackList: (() -> List<ActionBean>?)? = null

    var getActionById: ((actionId: Long) -> ActionBean?)? = null

    var getCheckById: ((checkId: Long) -> CheckBean?)? = null

    fun initAction(actionList: List<ActionBean>?): List<ActionBean>? {
        if (actionList == null) {
            return null
        }
        val result = mutableListOf<ActionBean>()
        actionList.forEach { action ->
            initAction(action, action)?.let { result.add(it) }
        }
        return result
    }

    /**初始化[ActionBean]
     * 检查[com.angcyo.selenium.bean.ActionBean.actionById] [checkId]
     * 检查[com.angcyo.selenium.bean.ActionBean.checkId]
     * */
    fun initAction(actionBean: ActionBean?, parentActionBean: ActionBean? = null): ActionBean? {
        if (actionBean == null) {
            return null
        }
        val newAction = if (actionBean.actionById > 0) {
            getActionById?.invoke(actionBean.actionById)?.copyByJson(ActionBean::class.java)?.apply {
                if (actionBean.actionId > 0) {
                    //原始id
                    actionId = actionBean.actionId
                }
                if (!actionBean.enable) {
                    enable = actionBean.enable
                }
                if (actionBean.autoEnable) {
                    autoEnable = actionBean.autoEnable
                }
                //原始的标题
                title = actionBean.title ?: title
                des = actionBean.des ?: des
                start = actionBean.start ?: start
                after = actionBean.after ?: after
                before = actionBean.before ?: before
                config = actionBean.config ?: config
                check = actionBean.check ?: check
                conditionList = actionBean.conditionList ?: conditionList

                relyList = actionBean.relyList ?: relyList
            }
        } else {
            null
        }

        val result = if (newAction == null) {
            initCheck(actionBean)
            actionBean.before = initAction(actionBean.before, actionBean)
            actionBean.after = initAction(actionBean.after, actionBean)
            actionBean
        } else {
            newAction
        }

        result.before?.apply {
            if (relyList == null) {
                relyList = parentActionBean?.relyList
            }
            if (actionId == -1L) {
                actionId = parentActionBean?.actionId ?: actionId
            }
        }
        result.after?.apply {
            if (relyList == null) {
                relyList = parentActionBean?.relyList
            }
            if (actionId == -1L) {
                actionId = parentActionBean?.actionId ?: actionId
            }
        }
        return result
    }

    fun initCheck(actionBean: ActionBean) {
        actionBean.apply {
            if (check == null && checkId > 0) {
                check = getCheckById?.invoke(checkId)
            }
        }
    }
}

/**初始化任务中的[CheckBean]*/
fun TaskBean.initTask(action: DslTask.() -> Unit): TaskBean {
    val task = DslTask()
    task.action()
    initTask(task)
    return this
}

fun TaskBean.initTask(task: DslTask): TaskBean {
    before = task.initAction(before)
    after = task.initAction(after)
    actionList = task.initAction(actionList)

    val otherBackList = task.getBackList?.invoke()
    if (!otherBackList.isNullOrEmpty()) {
        backActionList = if (backActionList == null) {
            otherBackList.toList() //新对象
        } else {
            backActionList?.toMutableList()?.apply {
                addAll(otherBackList)
            }
        }
    }
    backActionList = task.initAction(backActionList)
    return this
}

fun ActionBean.initAction(action: DslTask.() -> Unit): ActionBean? {
    val task = DslTask()
    task.action()
    return task.initAction(this)
}

fun List<ActionBean>.initAction(action: DslTask.() -> Unit): List<ActionBean>? {
    val task = DslTask()
    task.action()
    return task.initAction(this)
}

fun ActionBean.initAction(task: DslTask): ActionBean? {
    return task.initAction(this)
}

fun List<ActionBean>.initAction(task: DslTask): List<ActionBean>? {
    return task.initAction(this)
}


